import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

class ImageSwiper extends StatefulWidget {
  const ImageSwiper({
    super.key,
    required this.images,
    this.height = 188,
    this.padding = const EdgeInsets.all(0),
    this.autoPlay = true,
    this.backgroundColor = Colors.white,
    this.dotActiveColor = Colors.white,
    this.dotDefaultColor = const Color(0xFFD9D9D9),
    required this.borderRadius,
  });

  // 图片集合
  final List<Widget> images;
  final double height;
  final EdgeInsets padding;
  final bool autoPlay;
  final Color backgroundColor;
  final Color dotActiveColor;
  final Color dotDefaultColor;
  final BorderRadius borderRadius;
  @override
  State<ImageSwiper> createState() => _ImageSwiperState();
}

class _ImageSwiperState extends State<ImageSwiper> {
  int idx = 0;

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.bottomCenter,
      children: [
        Container(
          clipBehavior: Clip.antiAliasWithSaveLayer,
          padding: widget.padding,
          decoration: BoxDecoration(
            color: widget.backgroundColor,
            borderRadius: widget.borderRadius,
          ),
          child: CarouselSlider(
            disableGesture: true,
            options: CarouselOptions(
              autoPlay: widget.autoPlay,
              height: widget.height,
              viewportFraction: 1,
              onPageChanged: (int i, CarouselPageChangedReason c) {
                setState(() {
                  idx = i;
                });
              },
            ),
            items: widget.images,
          ),
        ),
        Positioned(
          bottom: 10,
          child: Row(
            children: List.generate(widget.images.length, (index) => index).map((i) => Container(
              width: idx == i ? 16 : 8,
              height: 4,
              margin: i == widget.images.length - 1 ? const EdgeInsets.all(0) : const EdgeInsets.fromLTRB(0, 0, 8, 0),
              decoration: BoxDecoration(
                // color: Colors.white.withOpacity(idx == i ? 1 : 0.4),
                color: idx == i ? widget.dotActiveColor : widget.dotDefaultColor,
                borderRadius: BorderRadius.circular(2),
              ),
            )).toList(),
          ),
        ),
      ],
    );
  }
}
